$('.select2').select2();

// $('#datepicker').datetimepicker({
// 	format:'YYYY-MM-DD HH:mm:ss'
//  });

getData();

function getData() {
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ActivityName",
        async: true,
        data: {},
        success: function (data) {
            var box = $("#pro_act");
            box.empty();
            for (var i = 0; i < data.length; i++) {
                var text = `<option value='${data[i]}'>${data[i]}</option>`;
                box.append(text);
            }
        }
    });
}

function pic_replace(dom1, dom2) {
    var file = document.getElementById(dom1).files;
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function (e) {
        var Img = '<img class="img_sty" src="' + this.result + '" width="85px" height="100px"/>';
        $('.' + dom2).html(Img)
    }
}

function AddNewSize() {
    var num = $('.size').eq(2).val();
    var color = $('.size').eq(0).val();
    var size = $('.size').eq(1).val();

    if (num === '') {
        layer.msg('请填写数量');

    } else if (isNaN(num)) {
        layer.msg('请填写数字');

    } else {
        var addFlag = true;
        var tbody = $('.format_data').eq(0);
        var rows = tbody.find('tr');
        for (var i = 0; i < rows.length; i++) {
            var cells = rows[i].cells;
            if ((color === cells[0].innerHTML) && (size === cells[1].innerHTML)) {
                layer.msg('已添加此规格');
                addFlag = false;
                break;
            }
        }

        if (addFlag) {

            var stock = Number($("#Stock").val()) + Number(num);
            $("#Stock").val(stock);

            $('#kind_box').css('display', 'block');
            var trText = `<tr>
		      		    		<td>${color}</td>
		      		    		<td>${size}</td>
		      		    		<td>${num}</td>
		      		    		<td>
		      		    			<div>
		      		    				<button type="button" onclick="format_delete(event)">删除</button>
		      		    			</div>
		      		    		</td>
		      		    	</tr>`;
            $('.format_data').append(trText);

            // format_delete();
        }
    }
}


function format_delete(e) {
    // $('.format_delete').click(function(){
    //           	console.log("执行了")
    var num = e.currentTarget.parentNode.parentNode.parentNode.children[2].innerHTML;
    var stock = Number($("#Stock").val()) - Number(num);
    $("#Stock").val(stock);

    e.currentTarget.parentNode.parentNode.parentNode.remove();
    if ($('.format_data').eq(0).find('tr').length === 0) {
        $('#kind_box').css('display', 'none');
    }
    // })
    // }

}

$('.submit_all').click(function () {
    //上架时间
    //   var timeKind= $('input:radio[name="OnlineModel"]:checked').val();

    // var Text = $('.kind_box_3').find('input');
    // var Kind_text = $('.kind_box_2').find('input');
    var rows = $('.format_data').eq(0).find('tr');
    var textOnoff = true;
    var re = /^\d+\.?\d{0,2}$/;

    //判断必填信息
    if ($('input[name=Name]').val() === '' && textOnoff === true) {
        textOnoff = false;
        layer.msg('请填写商品名称')
        return;
    }
    if ($('input[name=Price]').val() === '' && textOnoff === true) {
        textOnoff = false;
        layer.msg('请填写商品价格')
        return;
    }
    if (!re.test($("#price").val())) {
        textOnoff = false;
        layer.msg('商品价格不正确')
        return;
    }
    if ($('input[name=referPrice]').val() === '' && textOnoff === true) {
        textOnoff = false;
        layer.msg('请填写市场参考价格')
        return;
    }
    if ($('input[name=Brand]').val() === '' && textOnoff === true) {
        textOnoff = false;
        layer.msg('请填写商品品牌')
        return;
    }
    if (rows.length == 0 && textOnoff === true) {
        layer.msg('请添加规格');
        textOnoff = false;
        return;
    }
    if ($('.file_img1').text() === '+' && textOnoff === true) {
        layer.msg('请添加商品缩略图');
        textOnoff = false;
        return;
    }
    if ($('.detail_pic').text() === '+' && textOnoff === true) {
        layer.msg('请添加商品详情介绍图');
        textOnoff = false;
    } else {

        for (var i = 0; i < $('.pic_l').length; i++) {
            if ($('.pic_l').eq(i).text() === '+') {
                layer.msg('请添加商品轮播图至5张');
                textOnoff = false;
            }
        }

    }

    if (textOnoff) {
        var pic_num = 0;//轮播图数量
        for (var i = 0; i < $('.pic_l').length; i++) {
            if ($('.pic_l').eq(i).find('img').length === 1) {
                pic_num += 1;
            }
        }

        var formData = new FormData(document.getElementById('new_form'));
        formData.append('ProductName', $('input[name=Name]').val());
        formData.append('ProductPrice', $('input[name=Price]').val());
        formData.append('ReferPrice', $('input[name=referPrice]').val());
        formData.append('ProductBrand', $('input[name=Brand]').val());
        formData.append('ProductType', $('#pro_type').val());
        formData.append('ProductGroup1', $('#group1').val());
        formData.append('ProductGroup2', $('#group2').val());
        formData.append('ProductActive', $('#pro_act').select2('val').join('_'));
        var size = [];
        var stock = 0;
        for (var r = 0; r < rows.length; r++) {
            var cell = rows[r].cells;
            size[r] = [];
            for (var c = 0; c < cell.length - 1; c++) {
                size[r][c] = cell[c].innerHTML;
            }
        }
        for (var r = 0; r < size.length; r++) {
            stock += Number(size[r][2]);
        }
        console.log(size);
        formData.append('ProductSize', JSON.stringify(size));
        formData.append('ProductStock', stock);
        formData.append('ImageNumber', pic_num);

        $.ajax({
            type: "POST",
            url: "http://www.puer.zone:30000/ProductAdd/",
            async: true,
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data === 'success') {
                    layer.msg('提交成功', {time: 500}, function () {
                        location.reload()
                    });
                } else if (data[1] === 'do not add again') {
                    layer.msg('商品名称重复');
                } else {
                    layer.msg('提交失败');
                }
            }
        });
    }
});

function group1Change() {
    var man = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣', '夹克', '中华立领', '中式'];
    var child = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣'];
    var work = ['男士西装', '男士衬衫', '男士裤子', '男士大衣', '男士夹克', '女士西服', '女士衬衣', '女士短裙', '大衣', '风衣'];
    var group2 = [];
    var group1 = $("#group1").val();
    if ((group1 === "男装") || (group1 === "女装")) {
        group2 = man;
    } else if (group1 === "童装") {
        group2 = child;
    } else if (group1 === "职业装") {
        group2 = work;
    }

    $("#group2").empty();
    for (var i = 0; i < group2.length; i++) {
        var text = `<option value='${group2[i]}'>${group2[i]}</option>`;
        $("#group2").append(text);
    }
}
	